package com.situ.personal.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.personal.entity.Department;
import com.situ.personal.entity.PageInfo;
import com.situ.personal.entity.Role;
import com.situ.personal.service.RoleService;

@WebServlet("/role")
public class RoleServlet extends HttpServlet{
	RoleService roleService = new RoleService();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String roleInfo = req.getParameter("method");
		if (roleInfo==null || "".equals(roleInfo)) {
			roleInfo="insertDepartmentInfo";
		}
		switch (roleInfo) {

		case "findByPage":
			findByPage(req,resp);
			break;
		case "deleteById":
			deleteById(req,resp);
			break;
		case "insertRole":
			insertRole(req,resp);
			break;
		case "selectById":
			selectById(req,resp);
			break;
		case "updateRoleInfo":
			updateRoleInfo(req,resp);
			break;

		default:
			break;
		}
		
	}


	private void updateRoleInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String sn = req.getParameter("sn");
		
		boolean isUpdate = roleService.updateRoleInfo(Integer.parseInt(id), name, sn);
		if (isUpdate) {
			System.out.println("Role信息修改成功");
		}else {
			System.out.println("Role信息修改失败");
		}
		
		resp.sendRedirect(req.getContextPath()+"/role?method=findByPage");
		
	}


	private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String roleId = req.getParameter("id");
		
		Role role = roleService.selectById(Integer.parseInt(roleId));
		
		req.setAttribute("role", role);
		req.getRequestDispatcher("/role_update.jsp").forward(req, resp);
		
	}


	private void insertRole(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		String RoleName = req.getParameter("name");
		String RoleSn= req.getParameter("sn");
		
		boolean isInsert = roleService.insertRole(RoleName,RoleSn);
		if (isInsert) {
			System.out.println("Role角色信息插入成功");
		}else {
			System.out.println("Role角色信息插入失败");
		}
		
		resp.sendRedirect(req.getContextPath()+"/role?method=findByPage");
		
	}


	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		int id = Integer.parseInt(req.getParameter("id"));
		
		boolean isDelete = roleService.deleteById(id);
		if (isDelete) {
			System.out.println("删除成功");
		}else {
			System.out.println("删除失败");
		}
		
		resp.sendRedirect(req.getContextPath()+"/role?method=findByPage");
	}


	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (pageNo==null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize==null || "".equals(pageSize)) {
			pageSize = "5";
		}
		
		PageInfo pageInfo = (PageInfo)roleService.findByPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
		
		req.setAttribute("RolePageInfo", pageInfo);
		req.getRequestDispatcher("/role_page_list.jsp").forward(req, resp);
		
	}
		
	
}
